class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int len = prices.size();
        vector<int> f(len + 1);
        auto g = f;
        f[0] = -prices[0], g[0] = 0;
        for (int i = 1; i < len; i++) {
            f[i] = max(f[i - 1], g[i - 1] - prices[i]);
            g[i] = max(f[i - 1] + prices[i] - fee, g[i - 1]);
        }
        return max(f[len - 1], g[len - 1]);
    }
};